﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using RED_NET_Core.BLL;
using RED_NET_Core.Utilty;
using RED_NET_Core.Entity;
namespace RED_NET.ContentPage
{
    public partial class PublicNews : System.Web.UI.Page
    {
        private static Users user;
        public static int isEdit;
        public static int newsId;
        public static int visitId;
        public void Init()
        {
            Session["PageName"] = "PublicDaily";
            newsId = 0;
             if (Session["USER"] != null)
            user = (Users)Session["USER"];
             else
                 Response.Redirect("~/ContentPage/Login_2.aspx");
            if (Request.QueryString["VISIT_ID"] != null)
                visitId = Int32.Parse(Request.QueryString["VISIT_ID"]);
            else
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            if(visitId!=user.UserId)
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            Label_Content_Error.Text = "";
            Label_Title_Error.Text = "";
            if (Request.QueryString["_IsEdit"] != null)
            {

                isEdit = Int32.Parse(Request.QueryString["_IsEdit"]);
                if (Request.QueryString["_NewsId"] != null)
                    newsId = Int32.Parse(Request.QueryString["_NewsId"]);
                else
                    Response.Redirect("~/ContentPage/Error/PageError.aspx");
            }
            else
                isEdit = 0;

            if (isEdit == 1 && !IsPostBack)
            {
                News _news = new News();
                try
                {
                     _news = News_BLL.ShowNews(newsId);
                }
                catch (Exception ex)
                {
                    Response.Redirect("~/ContentPage/Error/PageError.aspx");
                }
                
                if(_news.Level!=2||_news.UId!=user.UserId)
                    Response.Redirect("~/ContentPage/Error/PageError.aspx");
               TextBox_Title.Text = _news.Title;
               TextBox_Conent.Text = _news.Content;
               Label1.Text = "编辑日志";
               TextBox_Title.ReadOnly = false;
               TextBox_Conent.ReadOnly = false;

            }
            else if (isEdit == 0)
            {
                Label_Content_Error.Text = "";
                Label_Title_Error.Text = "";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Init();
        }

        protected void Button_Public_Click(object sender, EventArgs e)
        {
            Label_Title_Error.Text = "";
            Label_Content_Error.Text = "";
            string _content = TextBox_Conent.Text;
            string _title = TextBox_Title.Text ;
            _content = this.Server.HtmlEncode(_content).Replace("\n", " <br/>");//用HTML换行符替代Textbox中的换行符
            _content = this.Server.HtmlDecode(_content);
            ReturnValue resTitle = Safety.JudgeStr(_title, 1);
            ReturnValue resContent = Safety.JudgeStr(_content, 2);
            if (resTitle.value == 0 && resContent.value == 0)
            {

                if (isEdit == 0)
                {
                    try
                    {
                        newsId = News_BLL.AddDaily(user.UserId, _content, TextBox_Title.Text);
                    }
                    catch (Exception ex)
                    {
    
                        Response.Redirect("~/ContentPage/Error/PageError.aspx");
                    }
                    Response.Redirect("ShowDaily.aspx?_Id=" + newsId.ToString() + "&VISIT_ID=" + user.UserId.ToString());
                }
                else
                {
                    try
                    {
                        News_BLL.UpdateDaily(_content, TextBox_Title.Text.ToString(), newsId);
                    }
                    catch (Exception ex)
                    {
                    
                        Response.Redirect("~/ContentPage/Error/PageError.aspx");
                    }
                    Response.Redirect("ShowDaily.aspx?_Id=" + newsId.ToString() + "&VISIT_ID=" + user.UserId.ToString());

                }
            }
            else
            {
                Label_Title_Error.Text = resTitle.error;
                Label_Content_Error.Text = resContent.error;
            }
           
        }
    }
}
